﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using SklepISI.Core.Interfaces;
using SklepISI.Core.Models;
using SklepISI.Core.Services;
using SklepISI.Web.Models;
using SklepISI.Web.Providers;

namespace SklepISI.Web.Controllers
{
    [Authorize]
    public class OrderController : Controller
    {
        private IOrderService _orderService;
        private IProductRepository _productRepository;
        
        private CartSessionModel Cart
        {
            get
            {
                if (Session["StoredCart"] == null)
                {
                    var newCart = new CartSessionModel();
                    Session["StoredCart"] = newCart;
                }
                return Session["StoredCart"] as CartSessionModel;
            }
        }

        public OrderController(IOrderService orderService, IProductRepository productRepository)
        {
            _orderService = orderService;
            _productRepository = productRepository;
        }

        public ActionResult ApproveOrder()
        {
            var model = new OrderViewModel()
                                       {
                                           Products = PrepareOrderProducts(),
                                           DeliveryAddress = String.Empty
                                       };
            model.TotalAmount = CountAmount(model.Products);

            return View(model);
        }

        [HttpPost]
        public ActionResult ApproveOrder(OrderViewModel model)
        {
            var user = ((ISIMembershipProvider)Membership.Provider).GetUserByUserName(HttpContext.User.Identity.Name);



            if (ModelState.IsValid && user != null && Cart != null)
            {
                model.UserId = user.ID;
                model.Products = PrepareOrderProducts();

                _orderService.CreateOrder(new NewOrderModel()
                {
                    DeliveryAddress = model.DeliveryAddress,
                    Products = model.Products,
                    UserId = model.UserId
                });
                Session["StoredCart"] = null;

                return View("OrderComplete");

            }

            model.Products = PrepareOrderProducts();
            model.DeliveryAddress = String.Empty;
            model.TotalAmount = CountAmount(model.Products);

            return View(model);
        }

        private decimal CountAmount(ICollection<NewOrderProductModel> products)
        {
            decimal amount = 0;

            foreach (var p in products)
            {
                amount += p.Product.Price * p.Quantity;
            }

            return amount;
        }

        private List<NewOrderProductModel> PrepareOrderProducts()
        {
            var orderedProducts = new List<NewOrderProductModel>();

            if (Cart != null)
            {

                foreach (var p in Cart.Products)
                {
                    var product = _productRepository.GetProduct(p.Key);

                    if (product != null)
                    {
                        var orderContent = new NewOrderProductModel()
                                               {
                                                   Quantity = p.Value,
                                                   Product = product
                                               };

                        orderedProducts.Add(orderContent);
                    }

                }
            }

            return orderedProducts;

        }



    }
}
